dp hashing strings *2000

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int M=1e9+7;
const int N=5000;
int n;
string s;
int dp[N+2][N+2];
int pre[N+2][N+2];
int lcp[N+2][N+2];
int main()
{
    cin>>n>>s;
    for(int i=n-1;i>=0;i--){
        for(int j=i;j>=0;j--){
            if(s[i]==s[j])
                lcp[i][j]=lcp[i+1][j+1]+1;
            lcp[j][i]=lcp[i][j];
        }
    }
    dp[0][0]=0;
    for(int i=0;i<=n;i++){
        pre[0][i]=1;
    }
    for(int i=1;i<=n;i++){
        for(int len=1;len<=i;len++){
            if(s[i-len]!='0'){
            dp[i][len]=pre[i-len][len-1];
            int p2=i-len;
            int p1=i-2*len;
            if(p1<0||p2<0)
                continue;
            int x=lcp[p1][p2];
            if(x>=len)
                continue;
            if(s[p1+x]<s[p2+x])
                dp[i][len]=(dp[i][len]+dp[i-len][len])%M;
            }
        }
        for(int j=1;j<=n;j++){
            pre[i][j]=(pre[i][j-1]+dp[i][j])%M;
        }
    }
    cout<<pre[n][n]<<endl;
}


Comments

Submit
0 Comments
More Questions

1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer